package cn.shujuhai.cms.controller.channel;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.shujuhai.cms.domain.SysChannel;
import cn.shujuhai.cms.domain.SysChannelExportDTO;
import cn.shujuhai.cms.service.SysChannelService;
import cn.shujuhai.cms.util.SysChannelConverter;
import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.pluging.data.ResultVo;
import com.alibaba.excel.EasyExcel;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author: zhangDeZhong
 * @Date: 2024/12/5 12:01
 * @Description: 提供表【sys_channel(栏目)】的操作接口
 **/

@RestController
@RequestMapping("/cms/channel")
@RequiredArgsConstructor
public class SysChannelController {

    final SysChannelService sysChannelService;

    /**
     * 根据栏目ID获取栏目信息
     *
     * @param channelId 栏目ID
     * @return 栏目信息结果
     */
    @SysLog(value = "根据栏目ID获取栏目信息", operationType = "查询", module = "栏目模块")
    @GetMapping("/select")
    public ResultVo<?> getChannelById(@RequestParam(value = "channelId") Integer channelId) {
        SysChannel sysChannel = this.sysChannelService.getChannelById(channelId);
        return ResultVo.success(sysChannel);
    }

    /**
     * 更新栏目信息
     *
     * @param sysChannel 需要栏目信息
     * @return 结果信息
     */
    @SysLog(value = "修改栏目", operationType = "修改", module = "栏目模块")
    @PostMapping("/update")
    public ResultVo<?> updateSaveChannel(@RequestBody @Validated SysChannel sysChannel) {
        return this.sysChannelService.updateSaveChannel(sysChannel);
    }

    /**
     * 添加栏目信息
     *
     * @param sysChannel 需要添加的栏目信息
     * @return 结果信息
     */
    @SysLog(value = "新增栏目", operationType = "新增", module = "栏目模块")
    @PostMapping("/add")
    public ResultVo<?> addChannel(@RequestBody @Validated SysChannel sysChannel) {
        return this.sysChannelService.addChannel(sysChannel);
    }

    /**
     * 删除栏目信息
     *
     * @param channelId 需要删除的栏目信息
     * @return 结果信息
     */
    @SysLog(value = "删除栏目", operationType = "删除", module = "栏目模块")
    @PostMapping("/delete/{channelId}")
    public ResultVo<?> deleteChannel(@PathVariable Long channelId) {
        return this.sysChannelService.deleteChannel(channelId);
    }

    /**
     * 获取父级栏目树
     *
     * @return 父级栏目树响应结果
     */
    @SaCheckPermission("channel_list")
    @GetMapping("/getParentMenus")
    public ResponseEntity<List<Map<String, Object>>> getParentChannels() {
        // 获取父级栏目树形数据
        List<Map<String, Object>> parentMenus = sysChannelService.getParentMenusAsTree();
        return ResponseEntity.ok(parentMenus);
    }

    /**
     * 根据栏目ID判断栏目下是否有文章
     *
     * @return 父级栏目树响应结果
     */
    @SaCheckPermission("channel_list")
    @GetMapping("/judgeInfoExists")
    public ResultVo<?> judgeInfoExists(@RequestParam Long channelId) {
        return this.sysChannelService.judgeInfoExists(channelId);
    }

    /**
     * 获取栏目信息列表
     *
     * @return 栏目信息列表结果
     */
    @SaCheckPermission("role_list")
    @GetMapping("/getChannelList")
    public ResultVo<?> getChannelList() {
        return this.sysChannelService.getChannelList();
    }

    /**
     * 根据栏目ID获取栏目路径
     *
     * @param channelId 栏目ID
     * @return 栏目路径结果信息
     */
    @SaCheckPermission("info_admin")
    @GetMapping("/getChannelUrlById")
    public ResultVo<?> getChannelUrlById(@RequestParam(value = "channelId") Long channelId) {
        return this.sysChannelService.getChannelUrlById(channelId);
    }

    /**
     * 获取栏目所有的浏览量
     *
     * @return 返回一个ResultVo对象，其中包含总浏览量的信息
     */
    @SaCheckLogin
    @GetMapping("/getTotalCount")
    public ResultVo<?> getTotalViewCount() {
        return this.sysChannelService.getTotalViewCount();
    }

    /**
     * 获取栏目和文章的浏览量
     *
     * @return 返回一个ResultVo对象，其中包含栏目和文章的浏览量
     */
    @SaCheckLogin
    @GetMapping("/getChannelAndArticleViews")
    public ResultVo<?> getChannelAndArticleViews() {
        return this.sysChannelService.getChannelAndArticleViews();
    }

    @SaCheckLogin
    @GetMapping("/viewChannelRanking")
    public ResultVo<?> viewArticleRanking() {
        return this.sysChannelService.viewArticleRanking();
    }

    /**
     * 获取栏目模板代码
     *
     * @param channelId 栏目ID
     * @return 栏目模板代码结果信息
     */
    @SaCheckPermission("info_admin")
    @GetMapping("/getTemplateCode")
    public ResultVo<?> getTemplateCode(@RequestParam Long channelId) {
        return this.sysChannelService.getTemplateCode(channelId);
    }


    @PostMapping("/countChannelNum")
    public void countChannelNum(@RequestBody Map<String, String> body) {
        this.sysChannelService.countChannelNum(body);
    }

    @GetMapping("/export")
    public void exportSysChannel(HttpServletResponse response) throws IOException {
        sysChannelService.exportSysChannel(response);
    }

    @GetMapping("countChannel")
    public ResultVo<?> getCountChannel(String date) {
        return this.sysChannelService.getCountChannel(date);
    }

}